package xiaoa.java.push.push;
import java.util.HashSet;





import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.mail.internet.MimeMessage;

import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import xiaoa.java.log.L;
import xiaoa.java.push.bean.EmailMessageBean;



/**
 * 发送email
 * @author lkc
 * @date 2017年12月7日 上午9:52:44
 * @version V1.0
 *
 */
public class PushEmail {

	
	/**
	 * 发送器
	 */
	 JavaMailSenderImpl mailSender = null;
	
	 /**
	  * 邮件用户名
	  */
	 private  String emailSendUsername = null;
	
	 /**
	  *  邮箱服务器地址
	  */
	 private  String emailSendHost     = null;
	
	 /**
	  * 邮箱密码
	  */
	 private  String emailSendPassword = null;
	
	
	 /**
	  * 将mailSender 绑定到线程中
	  */
	 private ThreadLocal<JavaMailSenderImpl >  mailSenderLocal  = new ThreadLocal<JavaMailSenderImpl>();
	
	 /**
	  *
	  */
	 private Set<JavaMailSenderImpl>  mailSenderSet =  new HashSet<JavaMailSenderImpl>();
	
	
	 /**
	  * 
	  * 构造器
	  * <p>Title: </p>
	  * <p>Description: </p>
	  * @author xiaoa
	  * @param emailSendHost  邮件服务器 
	  * @param emailSendUsername  用户名
	  * @param emailSendPassword  密码
	  */
	 public PushEmail(String emailSendHost , String emailSendUsername , String emailSendPassword ){
		 
			this.emailSendHost     = emailSendHost;
			this.emailSendUsername = emailSendUsername;
			this.emailSendPassword = emailSendPassword;
			
			 // 创建一个邮件发送器
			 mailSender = new JavaMailSenderImpl();
		     mailSender.setHost(this.emailSendHost);
		     mailSender.setUsername(this.emailSendUsername);
			 mailSender.setPassword(this.emailSendPassword);
			 Properties javaMailProperties = new Properties();
			 javaMailProperties.setProperty("mail.smtp.timeout" , String.valueOf(TimeUnit.MINUTES.toMillis(5)));
			 javaMailProperties.setProperty("mail.smtp.connectiontimeout" , String.valueOf(TimeUnit.MINUTES.toMillis(1)));
			 
			 javaMailProperties.setProperty("mail.smtp.auth", "true");
			 javaMailProperties.put("mail.smtpstarttls.enable", "true");
			 mailSender.setJavaMailProperties(javaMailProperties);
			
	 }
	
	 /**
	  * 对象实例
	  * @Title: getJavaMailSenderImpl
	  * @return
	  * @throws Throwable
	  * @author xiaoa
	  */
	 private JavaMailSenderImpl  getJavaMailSenderImpl()throws Throwable{
		 
		 // 获取线程中的email发送器
		 JavaMailSenderImpl mailSender = mailSenderLocal.get();
		 
		 if (mailSender == null){
			 
			 // 创建一个邮件发送器
			 mailSender = new JavaMailSenderImpl();
		     mailSender.setHost(this.emailSendHost);
		     mailSender.setUsername(this.emailSendUsername);
			 mailSender.setPassword(this.emailSendPassword);
			 Properties javaMailProperties = new Properties();
			 javaMailProperties.put("mail.smtp.auth", "true");
			 javaMailProperties.put("mail.smtpstarttls.enable", "true");
			 mailSender.setJavaMailProperties(javaMailProperties);
			 
			 // 绑定到线程中
			 mailSenderLocal.set(mailSender);;
			 
			 mailSenderSet.add(mailSender);
			 
		 }
		 
		 return mailSender;
	 }
	
	
	 /**
	  * 发送email
	  * @Title: sendEmail
	  * @param bean
	  * @author xiaoa
	  */
	 public  void sendEmail(EmailMessageBean  bean)throws Throwable{
		 
		 // 创建一个 javax Emial message
		 MimeMessage msg  = getJavaMailSenderImpl().createMimeMessage();
		 
	     // 创建MimeMessageHelper对象，处理MimeMessage的辅助类
		 MimeMessageHelper helper = new MimeMessageHelper(msg, true, "UTF-8");
		 helper.setSubject(bean.getSubject());
		 helper.setFrom(emailSendUsername,bean.getPersonal());
		 helper.setTo(bean.getTo());
		 helper.setText(bean.getText(), bean.getHtml());
		 
		 // 发送消息
		 getJavaMailSenderImpl().send(msg);
	 }
	
	 
	 
	 public static void main(String[] args)throws Throwable {
		
//		 PushEmail  push = new PushEmail("smtp.163.com", "17740575090@163.com", "xiaoa123");
//		 
//		 EmailMessageBean  mail = new EmailMessageBean();
//		 
//		 mail.setPersonal("personal_test");
//		 mail.setSubject("setSubjectdada");
//		 mail.setTo("642631438@qq.com");
//		 mail.setText("dada");
//		 mail.setHtml(true);
//		 
//		 push.sendEmail(mail);		 
		 
		 L.info("发发送成功11");
		 
	}
	 
	
	}
